package io.renren.modules.generator.service.impl;

import com.baomidou.mybatisplus.service.impl.ServiceImpl;
import io.renren.common.utils.Constant;
import io.renren.common.utils.ShiroUtils;
import io.renren.common.vo.OrderGroupExportVo;
import io.renren.modules.generator.dao.OrdersGroupDao;
import io.renren.modules.generator.entity.OrdersGroupEntity;
import io.renren.modules.generator.service.OrdersGroupService;
import io.renren.modules.generator.service.OrdersService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.List;
import java.util.Map;


@Service("ordersGroupService")
public class OrdersGroupServiceImpl extends ServiceImpl<OrdersGroupDao, OrdersGroupEntity> implements OrdersGroupService {

    @Autowired
    private  OrdersGroupDao ordersGroupDao;

    @Autowired
    private OrdersService ordersService;


    @Override
    public  List<OrdersGroupEntity> queryPage(Map<String, Object> params) {

        return ordersGroupDao.queryPage(params);
    }
    @Override
    public  int queryPageTotal(Map<String, Object> params) {

        return ordersGroupDao.queryPageTotal(params);
    }

    @Override
    public void save(OrdersGroupEntity ordersGroupEntity) {


        ordersGroupEntity.setCreatedBy(ShiroUtils.getUserEntity().getUsername());
        LocalDateTime now = LocalDateTime.now();
        String format = now.format(DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss"));
        ordersGroupEntity.setCreatedTime(format);
        ordersGroupEntity.setStatus(Constant.OrderStatus.NEW.getValue());
        this.insert(ordersGroupEntity);
        ordersService.orderCreatedGroupByIds(ordersGroupEntity.getOrderIds(),ordersGroupEntity.getId());
    }

    @Override
    public void updateStatusById(String value, Integer id) {
        ordersGroupDao.updateStatusById(value,id);
    }

    @Override
    public void delete(int id) {
        ordersService.cancelOrderGroupByOrderGroupId(id);
        ordersGroupDao.deleteById(id);


    }

    @Override
    public List<OrderGroupExportVo> queryDataForExcel(Map<String, Object> params) {
        return ordersGroupDao.queryDataForExcel(params);
    }

    @Override
    public int queryTotalForExcel(Map<String, Object> params) {
        return ordersGroupDao.queryTotalForExcel(params);
    }

    @Override
    public void updateDtAreaById(String toDtAreaIds, String toDtAreaNames, String fromDtAreaIds, String fromDtAreaNames, Integer id) {
        ordersGroupDao.updateDtAreaById(toDtAreaIds,toDtAreaNames,fromDtAreaIds,fromDtAreaNames,id);
    }


}
